package game;

import javax.swing.JOptionPane;

// konstruerer sub-klassen Ownable af klassen Field.
public class Ownable extends Field{
	private int			price;
	private int			rentToPay;
	private Player		owner;
	private boolean	mortgage;
	private boolean	forceBuy;

	//Metoden getRent
	public int getRent(){
		return rentToPay;
	}

	public boolean setForceToBuy(boolean forceIt) {
		forceBuy = forceIt;
		return forceIt;
	}

	//Metode for feltets købepris
	public int getPrice()
	{
		return price;
	}

	//set-metode for int-prisen af Ownable
	public void setPrice(int price)
	{
		this.price = price;
	}

	//get-metode for ejeren af feltet
	public Player getOwner()
	{
		return owner;
	}

	//set-metode for et ownable-felt, hvor spiller sættes lig variablen owner.
	public void setOwner(Player player)
	{
		owner = player;
	}

	public void landOnField(Player player){

		//Hvis der er ingen ejer af feltet	
		if (owner == null) {

			//Hvis spilleren har penge nok, kan han vælge at købe feltet
			if ((player.getAccount().getPoint() >= price)){

				int valg = 0;
				if (!forceBuy)
				{
					valg = JOptionPane.showConfirmDialog(null, "Vil du købe denne grund:\n" + getName() + " for " + price + "?", "Køb grund ...", JOptionPane.YES_NO_OPTION);;//Valg==0 er ja
				}

				//hvis spilleren vælger at købe feltet, trækkes prisen fra hans konto
				if (valg == 0) {
					owner = player;
					player.getAccount().addPoints(-price);
					System.out.println(" >> Lot bought by: " + player.getName());
				}
			}
		}
		//hvis feltet ejes af en anden spiller, kaldes getRent fra subklasserne af Ownable, så man betaler til ejeren af fetet
		else if (owner != player) {
			player.getAccount().addPoints(-getRent());
			owner.getAccount().addPoints(getRent());
			System.out.println("Afregning?");
		}	
	}
	public void changeMortgage() {
		if (mortgage == true){
			int costPrice = (int) ((price/2) + price*0.1);
			if (getOwner().getAccount().getPoint() >= costPrice){
				getOwner().getAccount().addPoints(-costPrice);
			}
			mortgage = false;
		}
		else {
			if (this instanceof Street){
				int property =0;
				Street thisAsStreet = (Street) this;
				System.out.println("supersejtgrpnumer" + thisAsStreet.getGroup());
				for (int i = 0; i < Board.fields.length; i++) {
					if ((Board.fields[i] instanceof Street) && ((Street) Board.fields[i]).getGroup() == thisAsStreet.getGroup()) {
						property = property + ((Street) Board.fields[i]).getProperty();
					}
				}
				Board.sellPropertyGroup(thisAsStreet.getOwner(), thisAsStreet.getGroup(), property);
				mortgage = true;
				getOwner().getAccount().addPoints(price/2);
			}
			else{
				mortgage = true;
				getOwner().getAccount().addPoints(price/2);
			}
		}
	}


	public boolean getMortgage(){
		return mortgage;
	}

}
